<template>
  <div>
    <BasicTable @register="registerTable" :can-resize="true" :bordered="true" :striped="false">
      <template #toolbar>
        <a-button type="link" @click="preViewLed">预览</a-button>
        <a-button type="primary" @click="onAddLedView"> 新增 </a-button>
      </template>
      <!-- 是否固定显示 -->
      <template #fixedFlag="{ record }">
        <template v-if="record.fixed === true">
          <Tag :color="'cadetBlue'">是</Tag>
        </template>
        <template v-if="record.fixed === false">
          <Tag :color="'DarkGray'">否</Tag>
        </template>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditLedView.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: onDeleteLedView.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>

    <addOrUpdate @register="registerAddOrUpdateForm" @success="afterSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { Tag } from 'ant-design-vue';

  import { useMessage } from '/@/hooks/web/useMessage';

  import { getAllLedView, deleteLedView } from '/@/api/cms/ledview';
  import { ledViewTableColumns } from './ledview.data';

  //引入子组件
  import addOrUpdate from './AddOrUpdate.vue';

  //使用modal弹出窗口
  import { useModal } from '/@/components/Modal';
  const { createMessage } = useMessage();

  const [registerAddOrUpdateForm, { openModal: openAddOrUpdateForm }] = useModal();

  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';

  export default defineComponent({
    name: 'LedViewManagement',
    components: { Tag, BasicTable, TableAction, addOrUpdate },
    setup() {
      const moduleTitle = ref('Led画面管理');

      /**
       * 请求后台查询接口返回数据显示
       */
      const [registerTable, { reload }] = useTable({
        title: moduleTitle.value,
        api: getAllLedView,
        columns: ledViewTableColumns, //表头数据
        showTableSetting: true, //是否显示表格设置
        showIndexColumn: false,
        actionColumn: {
          width: 100,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
        pagination: true, //关闭分页
      });

      /**
       * 打开新增窗口
       */
      function onAddLedView() {
        //使用props传递数据至子组件，在data参数中指定
        let propsData: QcUnifyTransToChildProps = {
          moduleName: moduleTitle.value,
          operateType: OperationTypeEnum.ADD,
        };

        openAddOrUpdateForm(true, propsData);
      }

      /**
       * 打开编辑窗口
       */
      function onEditLedView(record: Recordable) {
        let propsData: QcUnifyTransToChildProps = {
          moduleName: moduleTitle.value,
          operateType: OperationTypeEnum.EDIT,
          dataId: record.id,
        };
        openAddOrUpdateForm(true, propsData);
      }

      /**
       * 删除
       */
      function onDeleteLedView(record: Recordable) {
        deleteLedView(record.id)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              afterSuccess();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      /**
       * 预览led画面配置
       */
      function preViewLed() {
        //打开一个新的浏览器窗口
        const url = window.location.protocol + '//' + window.location.host + '/led/';
        window.open(url);
      }

      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
      }

      return {
        moduleTitle,
        registerTable,
        afterSuccess,
        registerAddOrUpdateForm,
        onAddLedView,
        onEditLedView,
        onDeleteLedView,
        preViewLed,
      };
    },
  });
</script>
